"ui";
importClass("android.graphics.Rect");
importClass("android.graphics.Bitmap");
importClass(java.io.File);
importClass(java.io.FileOutputStream);
importClass(android.view.ViewGroup);
importClass(android.view.Gravity);
importClass(android.view.animation.ScaleAnimation);
importClass(android.view.animation.Animation);
importClass(android.widget.PopupWindow);

ui.layout(
  <vertical>
    <button id="guide" w="200dp">
      点击开启新手引导
    </button>
    <button id="aaa" margin="56">
      aaa
    </button>
    <button id="bbb" margin="56">
      bbb
    </button>
    <button id="ccc" margin="56">
      ccc
    </button>
  </vertical>
);

ui.guide.click(function () {
  log("开启新手引导");
  setTimeout(function () {
    新手引导();
  }, 500);
});

function 新手引导() {
  // 设置新手引导的控件
  let dataList = [
    {
      view: ui.aaa,
      introduction: "第一个控件",
    },
    {
      view: ui.bbb,
      introduction: "第二个控件",
    },
    {
      view: ui.ccc,
      introduction: "第三个控件",
    },
  ];
  // 提取控件位置信息
  dataList = dataList.map(function (item) {
    return {
      left: item.view.getLeft(),
      top: item.view.getTop(),
      right: item.view.getRight(),
      bottom: item.view.getBottom(),
      view: item.view,
      introduction: item.introduction,
    };
  });
  // 添加蒙板
  addMaskView(dataList);
}

function addMaskView(dataList) {
  // 不包含通知栏的蒙板
  // 蒙板是一张图片, 放在popWindow布局中
  // canvas画一张当前屏幕的截图
  // 再把其他地方涂成半透明灰色
  // 突出引导按钮
  screenCapture(activity);
  // 第一步: 当前屏幕截屏
  let dirName = "/sdcard/111/";
  let filename = "111.png";
  saveBitmap(bmp, dirName, filename);
  log("查看图片");
  let imgpath = dirName + filename;
  // app.viewFile(files.join(dirName, filename));
  let { mPopWindow, contentView } = addPopWindow();
  let imgView = contentView.img;

  threads.start(function () {
    var len = dataList.length;
    for (var i = 0; i < len; i++) {
      let data = dataList[i];
      data.number = i;
      let compositeImgpath = compositeImg(data, imgpath);
      ui.run(function () {
        imgView.attr("src", "file://" + compositeImgpath);
      });
      sleep(2000);
    }
    ui.run(function () {
      mPopWindow.dismiss();
    });
  });
}

function addPopWindow() {
  let contentView = ui.inflate(
    <vertical>
      <img id="img" layout_width="match_parent" layout_height="match_parent"></img>
    </vertical>,
    null,
    false
  );
  let mPopWindow = new PopupWindow(
    contentView,
    ViewGroup.LayoutParams.MATCH_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT,
    true
  );
  ui.run(function () {
    mPopWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    动画(contentView);
  });
  return {
    mPopWindow: mPopWindow,
    contentView: contentView,
  };
}
function 动画(view) {
  let myAnim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5, 1, 0.5);
  myAnim.setDuration(500);
  view.startAnimation(myAnim);
}
function screenCapture(activity) {
  log("screenCapture");
  // 获取windows中最顶层的view
  view = activity.getWindow().getDecorView();
  view.buildDrawingCache();

  // 获取状态栏高度
  rect = new Rect();
  view.getWindowVisibleDisplayFrame(rect);
  statusBarHeights = rect.top;
  display = activity.getWindowManager().getDefaultDisplay();

  // 获取屏幕宽和高
  widths = display.getWidth();
  heights = display.getHeight();

  // 允许当前窗口保存缓存信息
  view.setDrawingCacheEnabled(true);

  // 去掉状态栏
  let bmp = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeights, widths, heights - statusBarHeights);

  // 销毁缓存信息
  view.destroyDrawingCache();

  return bmp;
}

function saveBitmap(bmp, dirName, fileName) {
  log("saveBitmap");
  let dir = new File(dirName);
  // 判断文件夹是否存在，不存在则创建
  if (!dir.exists()) {
    dir.mkdir();
  }

  let file = new File(dirName + fileName);
  log(dirName + fileName);
  // 判断文件是否存在，不存在则创建
  if (!file.exists()) {
    file.createNewFile();
  }

  let fos = null;
  fos = new FileOutputStream(file);
  if (fos != null) {
    // 第一参数是图片格式，第二个是图片质量，第三个是输出流
    bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
    // 用完关闭
    fos.flush();
    fos.close();
    log("保存完成");
  }
}
function compositeImg(data, imgpath) {
  let img = images.read(imgpath);

  let bitmap = android.graphics.Bitmap.createBitmap(
    img.getWidth(),
    img.getHeight(),
    android.graphics.Bitmap.Config.ARGB_8888
  );
  var canvas = new Canvas(bitmap);
  var paint = new Paint();
  canvas.drawARGB(0, 125, 125, 125);
  canvas.drawBitmap(img.getBitmap(), 0, 0, paint);
  // 画画遮罩
  canvas.drawARGB(200, 125, 125, 125);
  // 画画边框

  paint.setStyle(Paint.Style.STROKE); //空心矩形框
  paint.setStrokeWidth(20);
  paint.setColor(colors.parseColor("#0000ff"));
  canvas.drawRect(data.left, data.top, data.right, data.bottom, paint);

  // 裁剪指定区域的图片
  var clip = images.clip(img, data.left, data.top, data.right - data.left, data.bottom - data.top);
  let smallBitmap = clip.getBitmap();
  // 画画指定控件
  canvas.drawBitmap(smallBitmap, data.left, data.top, paint);
  // 添加文字
  paint.setTextAlign(Paint.Align.CENTER);
  paint.setTextSize(70);
  paint.setStyle(Paint.Style.FILL);
  paint.setColor(colors.parseColor("#f000ff"));
  canvas.drawText(data.introduction, data.left + parseInt((data.right - data.left) / 2), data.bottom + 100, paint);
  img = canvas.toImage();
  let compositeImgpath = imgpath + data.number + ".png";
  images.save(img, compositeImgpath, "png", 100);
  return compositeImgpath;
}
